﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Photon.SocketServer;
using WorldOfSuperpowers_COMMON.DataModel;

namespace WorldOfSuperpowers.Cache
{
    public class MatchCenter
    {
        #region 单例
        private static MatchCenter instance;

        private MatchCenter()
        {

        }

        public static MatchCenter GetInstance()
        {
            if (instance == null)
            {
                instance = new MatchCenter();
            }
            return instance;
        }
#endregion
        
        //在单排1v1队列中的玩家
        public List<string> PlayerInMatchListForTwo = new List<string>();

        //目前进行中的1v1房间
        public List<MatchRoomForTwo> TwoPlayerRoomList = new List<MatchRoomForTwo>();

        /// <summary>
        /// 添加到匹配队列
        /// </summary>
        /// <param name="UserAccount">匹配玩家账号</param>
        public void PutNewPlayerInQueueForTwo(string UserAccount)
        {
            //添加进列表，检测是否能匹配到人，如果匹配得到，在全局客户端字典中调取发送匹配成功事件
            PlayerInMatchListForTwo.Add(UserAccount);
            if (PlayerInMatchListForTwo.Count >= 2)
            {
                string P1 = PlayerInMatchListForTwo[0];
                string P2 = PlayerInMatchListForTwo[1];
                PlayerInMatchListForTwo.RemoveAt(0);
                PlayerInMatchListForTwo.RemoveAt(0);
                //开始给双方发送对方的角色信息
                MasterServer.log.Debug("找到两位玩家，开始进入匹配");
                MatchRoomForTwo room = new MatchRoomForTwo(new[] {P1, P2});
            }
            else
            {
                MasterServer.log.Debug("玩家不足，等待玩家加入匹配队列");
            }
        }

        /// <summary>
        /// 从匹配队列中移除
        /// </summary>
        public void RemovePlayerFromQueue(string UserAccount)
        {
            if (PlayerInMatchListForTwo.Contains(UserAccount))
            {
                PlayerInMatchListForTwo.Remove(UserAccount);
            }
            else
            {
                MasterServer.log.Debug("玩家:"+UserAccount+"不在队列中，无需移除");
            }
        }

        /// <summary>
        /// 收到房间内的准备消息
        /// </summary>
        /// <param name="roomIndex"></param>
        /// <param name="PlayerIndex"></param>
        public void GetRoomReadyMessage(ushort roomIndex,ushort PlayerIndex)
        {
            TwoPlayerRoomList[roomIndex].GetPlayerReadyMessage(PlayerIndex);
        }

        /// <summary>
        /// 收到房间内BP消息
        /// </summary>
        public void GetRoomBPMessage(SkillModel model)
        {
            TwoPlayerRoomList[model.MatchRoomIndex].GetBPMessage(model);
        }
    }
}
